﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

// std headers
#include <memory>
#include <vector>

// qt headers
#include <QString>
#include <QObject>

// gcmp headers

// custom headers
#include "GcsService.h"
#include "Major.h"
#include "ILegend.h"
#include "OwnerPtr.h"


class QNetworkAccessManager;
class QNetworkRequest;

namespace gmep
{
    class IGmepCategoryInfo;
}

namespace gmepgcs
{
    template<typename TResult> class CustomReply;
    class ICategoryProperty;
    struct IGmepGcsCategory;
    template<typename TData> class IPagedData;
}

namespace gmepgcs
{
    class GCS_SERVICE_EXPORTS PropertyService : public QObject
    {
        Q_OBJECT

    private:
        explicit PropertyService();
        PropertyService(const PropertyService &) = default;
        PropertyService &operator=(const PropertyService &) = default;
        virtual ~PropertyService();

    public:
        static PropertyService *Get();

    public:
        std::shared_ptr<ICategoryProperty> GetCategoryProperty(
            const std::wstring &categoryCode) const;

        std::vector<std::shared_ptr<IGmepGcsCategory>> GetCategories();
            /*const Major &major,
            const std::wstring &softwareVersion);*/
        
        std::vector<std::shared_ptr<ILegend>> GetLegends(
            const std::wstring & categoryCode,
            const ILegend::LegendType & legendType) const;

        CustomReply<IPagedData<std::vector<std::shared_ptr<ILegend>>>>* GetLegendsFromCloud(
            const std::wstring & categoryCode,
            const ILegend::LegendType & legendType,
            const int32_t & pageSize,
            const int32_t & page,
            const std::wstring &softwareVersion) const;

        std::vector<std::shared_ptr<ILegend>> GetLegendsFromEnterpriseCloudByHttpRespone(
            const std::wstring & categoryGuid,
            const std::wstring & enterpriseId,
            const ILegend::LegendType & legendType,
            const int32_t & pageSize,
            const int32_t & page,
            const std::wstring &softwareVersion) const;

        
        std::vector<std::shared_ptr<IGmepGcsCategory>> GetFullCategories(
            std::wstring leafCategoryCode) const;

        std::shared_ptr<IGmepGcsCategory> GetCategoryByCode(const std::wstring &categoryCode);

        const gmep::IGmepCategoryInfo* GetEndGmepCategoryByCode(const std::wstring &categoryCode);
    private:
        void ConfigureAuthorization(QNetworkRequest &request) const;

    private:
        void Init();
        static PropertyService *s_pInstance;
        std::shared_ptr<QNetworkAccessManager> m_spNetworkManager;
        QString m_gjwApiEndPoint;
        QString m_gdcEnterprisePoint;
        std::vector<std::shared_ptr<IGmepGcsCategory>> m_vCategories;

        std::map<std::wstring, gcmp::OwnerPtr<gmep::IGmepCategoryInfo>> m_stdCodeEndCategoryMap;
    };
}